package cn.com.panda.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import cn.com.panda.R;
import cn.com.panda.utils.ScreenUtils;

public class DialogWithProgress extends Dialog implements View.OnClickListener {

    private Button ok,cancel;
    private TextView contentTV;
    private ProgressBar progressBar;
    private Context mContext;
    private EventListener eventListener;
    private ProgressBar progressbar;
    private ImageView closeImageview;
    private String content;
    private int progressCurValue;
    private int progressMax;
    private boolean isShowProgress;


    private interface EventListener{
        void okBtnClick();
        void cancelBtnClick();
    }

    public DialogWithProgress(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public DialogWithProgress(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    public void setEventListener(EventListener eventListener) {
        this.eventListener = eventListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        //初始化界面控件
        initView();

        //初始化界面数据
        initData();
    }

    private void initData() {

        if(content != null)
            contentTV.setText(content);
    }

    public void setProgressBarShow(boolean show){
        this.isShowProgress = show;
    }

    public void setContent(String content) {
        this.content = content;

    }

    public void setProgressCurValue(int progressCurValue) {
        progressBar.setVisibility(View.VISIBLE);
        this.progressCurValue = progressCurValue;
        progressBar.setProgress(progressCurValue);
    }

    public void setProgressMax(int progressMax) {
        this.progressMax = progressMax;
    }

    private void initView(){
        ok = findViewById(R.id.yes);
        ok.setOnClickListener(this);

        cancel = findViewById(R.id.no);
        cancel.setOnClickListener(this);

        contentTV = findViewById(R.id.message);
        closeImageview = findViewById(R.id.titleclose);
        closeImageview.setOnClickListener(this);

        progressBar = findViewById(R.id.progressbar);
        progressBar.setMax(progressMax);
        progressBar.setVisibility(isShowProgress?View.VISIBLE:View.GONE);

    }



    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.yes:
                if(eventListener!=null)
                    eventListener.okBtnClick();
                this.cancel();
                break;
            case R.id.no:
                if(eventListener != null)
                    eventListener.cancelBtnClick();
                this.cancel();
                break;
            case R.id.titleclose:
                this.cancel();
                break;
        }
    }

    @Override
    public void show() {
        //设置dialog显示和退出动画
//        getWindow().setWindowAnimations(R.style.dialog_animation);
        super.show();
        //此处设置位置窗体大小，我这里设置为了手机屏幕宽度的3/4  注意一定要在show方法调用后再写设置窗口大小的代码，否则不起效果会
        getWindow().setLayout((ScreenUtils.getScreenWidth(mContext)/7*6), LinearLayout.LayoutParams.WRAP_CONTENT);
    }
}
